
package com.wa.nashorn;
import java.util.Map;
import javax.script.Compilable;
import javax.script.CompiledScript;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import jdk.nashorn.api.scripting.ScriptObjectMirror;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class NashornCompliedScriptHolder {
    private static final Logger LOGGER = LoggerFactory.getLogger(NashornCompliedScriptHolder.class);
    private ScriptEngine scriptEngine;
    private Map<String, ScriptObjectMirror> scriptObjectMirrorMap;

    public NashornCompliedScriptHolder() {
    }

    public ScriptEngine getScriptEngine() {
        return this.scriptEngine;
    }

    public ScriptObjectMirror compile(String key, String script) {
        try {
            CompiledScript compiledScript = this.getCompilable().compile(script);
            Object eval = compiledScript.eval();
            if (eval == null) {
                LOGGER.warn("There is NULL return value from script, key={}, script={}", key, script.length() > 10 ? script.substring(0, 10) + "..." : script);
                return null;
            } else {
                return (ScriptObjectMirror)eval;
            }
        } catch (ScriptException var5) {
            throw new ScriptCompileException("compile script error", var5);
        }
    }

    private Compilable getCompilable() {
        return (Compilable)this.scriptEngine;
    }

    public void setScriptEngine(ScriptEngine scriptEngine) {
        this.scriptEngine = scriptEngine;
    }

    public ScriptObjectMirror getScriptObjectMirror(String key) {
        return (ScriptObjectMirror)this.scriptObjectMirrorMap.get(key);
    }
}
